package com.tarena.lbs.user.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tarena.lbs.base.protocol.pager.BasePageQuery;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.user.dao.mapper.AdminMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.pojo.param.AdminLoginParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Repository
public class AdminRepositoryImpl implements AdminRepository {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public AdminPO login(AdminLoginParam param) {
        AdminPO adminPO= adminMapper.login(param.getPhone());
        return adminPO;
    }

    @Override
    public AdminPO detail(Integer id) {
        AdminPO adminPO= adminMapper.selectById(id);
        return adminPO;
    }

    @Override
    public AdminPO findByPhone(String accountPhone) {
        return adminMapper.login(accountPhone);
    }

    @Override
    public void save(AdminPO adminPO) {
        adminMapper.admininsert(adminPO);
    }

    @Override
    public PageResult<AdminVO> list(AdminQuery query) {
        // 1. 构建分页查询
        Page<AdminPO> page = new Page<>(query.getPageNo(), query.getPageSize());
        LambdaQueryWrapper<AdminPO> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(query.getNickname())) {
            wrapper.like(AdminPO::getNickname, query.getNickname());
        }

        if (StringUtils.isNotBlank(query.getAccountPhone())) {
            wrapper.eq(AdminPO::getAccountPhone, query.getAccountPhone());
        }

        if (query.getBusinessId() != null) {
            wrapper.eq(AdminPO::getBusinessId, query.getBusinessId());
        }


        // 2. 执行分页查询
        IPage<AdminPO> poPage = adminMapper.selectPage(page, wrapper);
        log.debug("查询结果：{}", poPage); // 使用 debug 级别日志

        // 3. 转换为 VO
        List<AdminVO> voList = poPage.getRecords().stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());

        // 4. 创建分页结果
        PageResult<AdminVO> pageResult = new PageResult<>(
                new BasePageQuery(query.getPageNo(), query.getPageSize())
        );
        pageResult.setObjects(voList);
        pageResult.setTotal(poPage.getTotal());

        return pageResult;
    }

    public AdminVO convertToVO(AdminPO po) {
        AdminVO adminVO = new AdminVO();
        adminVO.setId(po.getId());
        adminVO.setAccountPhone(po.getAccountPhone());
        adminVO.setAccountPassword(po.getAccountPassword());
        adminVO.setNickname(po.getNickname());
        adminVO.setEmail(po.getEmail());
        adminVO.setBusinessId(po.getBusinessId());
        adminVO.setAccountType(po.getAccountType());
        adminVO.setAccountStatus(po.getAccountStatus());
        adminVO.setCreateTime(po.getCreateTime());
        return adminVO;
    }
}
